<!doctype html>
{% load static %}
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>登录</title>
    <link href="{% static 'css/css.css' %}" rel="stylesheet" type="text/css">
    <script src="{% static 'admin/js/jquery.min.js' %}"></script>
</head>

<body>

{% include 'header.html'%}

<div class="login">
    <div class="login_left"><img src="{% static 'images/9.png' %}" alt=""/></div>
    <div class="login_right">
        <div class="login_tittle">用户登录</div>
        <form class="form">
            <input type="text" name="user_email" value="" class="yhm" placeholder="请输入邮箱号" autocomplete="off">
            <input type="password" name="user_pass" value="" class="mm" placeholder="请输入密码" autocomplete="off">
            <div class="yzm">
                <div class="yzm_left">
                    <input type="text" name="auth_code" value="" class="yan" placeholder="请输入验证码" autocomplete="off">
                </div>
                <div class="yzm_right">
                    <div class="auth_code">{{code}}</div>
                    <p>换一张</p>
                </div>

                <div class="zidong">
                    <div class="zidong_left">
                        <!--<input type="checkbox" style="margin-right:10px;"/>下次自动登录-->
                    </div>
                    <div class="zidong_right"><a href="resetPassword.html">忘记密码？</a></div>
                </div>
                <input type="button" class="deng" value="登录" onclick="login()"/>
                <div class="qita">
                    <div class="qita_line">其他登录方式</div>
                    <div class="qita_qq"><img src="{% static 'images/14.png' %}"/></div>
                </div>
            </div>
        </form>

    </div>
</div>

{% include 'footer.html'%}

</body>
<script>
    window.onload=function (){
        if("{{request.session.user}}"){
            window.location.href = "/"
        }
    }
    //登录
    function login(){
        let user_email = $("input[name='user_email']").val();
        let user_pass = $("input[name='user_pass']").val();
        let auth_code = $("input[name='auth_code']").val();
        if(isNotNull([user_email,user_pass,auth_code])){
            if(auth_code==$(".auth_code").html()){
                $.post(
                    "auth/login",
                    {
                        "user_email":user_email,
                        "user_pass":user_pass,
                    },
                    function (res){
                        if(res.code==200){
                            alert("登录成功！")
                            if(document.referrer != "http://127.0.0.1/reg.html"){
                                window.location.href = document.referrer
                            }else{
                                window.location.href = "/"
                            }
                        }else{
                            alert(res.message)
                        }
                    }
                )
            }else{
                alert("验证码错误！")
            }
        }else{
            alert("请完整填写表单！")
        }
    }

    //判断数据中值均不为空
    function isNotNull(arr){
        let flag = true
        for(let i=0;i<arr.length;i++){
            flag = arr[i]!==""
        }
        return flag
    }
</script>
</html>
